package com.cskaoyan.service;

import com.cskaoyan.bean.good.Goods;
import com.cskaoyan.bean.GrouponRules;
import com.cskaoyan.bean.GrouponRulesExample;
import com.cskaoyan.bean.vo.AdminAdForListDataBean;
import com.cskaoyan.bean.vo.AdminGrouponForListRecordVo;
import com.cskaoyan.bean.vo.SubGroupn;
import com.cskaoyan.mapper.GoodsMapper;
import com.cskaoyan.mapper.GrouponMapper;
import com.cskaoyan.mapper.GrouponRulesMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class AdminGrouponServiceImp implements AdminGrouponService {
    @Autowired
    GrouponMapper grouponMapper;

    @Autowired
    GrouponRulesMapper grouponRulesMapper;

    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public AdminAdForListDataBean<GrouponRules> queryGrouponRules(Integer page, Integer limit, String sort, String order, Integer goodsId) {
        PageHelper.startPage(page, limit);
        GrouponRulesExample grouponRulesExample = new GrouponRulesExample();
        GrouponRulesExample.Criteria criteria = grouponRulesExample.createCriteria();
        if (goodsId != null && !"".equals(goodsId)) {
            criteria.andGoodsIdEqualTo(goodsId);
        }
        criteria.andDeletedEqualTo(false);
        grouponRulesExample.setOrderByClause(sort + " " + order);
        List<GrouponRules> grouponRules = grouponRulesMapper.selectByExample(grouponRulesExample);
        return  new AdminAdForListDataBean<GrouponRules>(new PageInfo<>(grouponRules).getTotal(), grouponRules);
    }

    /**
     * t先要检验数据商品id是否正确，还有团购折扣是否为在0-1之间。
     * @param rules
     * @return
     */
    @Override
    public Integer addGrouponRules(GrouponRules rules) {
        Goods goods = goodsMapper.selectByPrimaryKey(rules.getGoodsId());
        if (goods == null || goods.getDeleted() == true) {
            return 500;
        }
        String dateTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        rules.setAddTime(dateTime);
        rules.setUpdateTime(dateTime);
        rules.setPicUrl(goods.getPicUrl());
        rules.setGoodsName(goods.getName());
        int result = grouponRulesMapper.insertSelective(rules);
        return result == 1 ? 200 : 404;
    }

    @Override
    public Integer updateGrouponRules(GrouponRules rules) {
        Goods goods = goodsMapper.selectByPrimaryKey(rules.getGoodsId());
        if (goods == null || goods.getDeleted() == true) {
            return 500;
        }
        String dateTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        rules.setUpdateTime(dateTime);
        rules.setPicUrl(goods.getPicUrl());
        rules.setGoodsName(goods.getName());
        Integer result = grouponRulesMapper.updateByPrimaryKeySelective(rules);
        return result == 1 ? 200 : 404;
    }

    @Override
    public Integer deleteGrouponRules(GrouponRules rules) {
        rules.setDeleted(true);
        int result = grouponRulesMapper.updateByPrimaryKeySelective(rules);
        return result == 1 ? 200 : 500;
    }

    @Override
    public AdminAdForListDataBean<AdminGrouponForListRecordVo> listRecord(Integer page, Integer limit, String sort, String order, Integer goodsId) {
        PageHelper.startPage(page, limit);
        List<AdminGrouponForListRecordVo> recordVoList = grouponMapper.selectObject(goodsId);
        for (AdminGrouponForListRecordVo adminGrouponForListRecordVo : recordVoList) {
            Integer grouponId = adminGrouponForListRecordVo.getGroupon().getGrouponId();
            List<SubGroupn> subGroupon =grouponMapper.selectSubGouponByGrouponId(grouponId);
            adminGrouponForListRecordVo.setSubGroupons(subGroupon);
        }
        return new AdminAdForListDataBean<AdminGrouponForListRecordVo>(new PageInfo<>(recordVoList).getTotal(), recordVoList);
    }
}
